// http://code.google.com/p/wpsm/
$(function(){
	session = {
		init: function(xml){
			session.editingItem = false;
			var xmlDom = $(xml);
			session.ID = xmlDom.find('session').attr('id');
			session.clientName = xmlDom.find('clientName').attr('first') + ' ' + xmlDom.find('clientName').attr('last');
			session.subjectName = xmlDom.find('subjectName').attr('first') + ' ' + xmlDom.find('subjectName').attr('last');
			session.photos.total = xmlDom.find('photos').attr('total');
			xmlDom.find('photograph').each(function(){
				session.photos[$(this).attr('id')] = {
					horiz: !!$(this).attr('horizontal'),
					a: $(this).attr('position')
				}
			});
			$('#login').slideUp(500,
			function(){
				$('#cart-tmpl').tmpl(session).appendTo('body').hide().slideDown(500);
				$('#photoChooser-tmpl').tmpl(session).appendTo('body').hide().slideDown(500);
				session.photoChooser.update();
				$('#photoChooser').disableTextSelect();
				$('#cart ul').disableTextSelect();
			});
		},
		photoSizes: [
		{
			name: 'Package C',
			shortIn: 4,
			longIn: 6,
			overlayName: 'package_c',
			shortOverlayPx: 263,
			longOverlayPx: 282,
			price: 125
		},
		{
			name: 'Package B',
			shortIn: 5,
			longIn: 7,
			overlayName: 'package_b',
			shortOverlayPx: 247,
			longOverlayPx: 217,
			price: 100
		},
		{
			name: 'Package A',
			shortIn: 5,
			longIn: 7,
			overlayName: 'package_a',
			shortOverlayPx: 227,
			longOverlayPx: 207,
			price: 75
		},
		{
			name: '11&#8221; x 14&#8221;',
			shortIn: 11,
			longIn: 14,
			overlayName: '11x14',
			shortOverlayPx: 222,
			longOverlayPx: 282,
			price: 20
		},
		{
			name: '8&#8221; x 10&#8221;',
			shortIn: 8,
			longIn: 10,
			overlayName: '8x10',
			shortOverlayPx: 162,
			longOverlayPx: 202,
			price: 10
		},
		{
			name: '5&#8221; x 7&#8221;',
			shortIn: 5,
			longIn: 7,
			overlayName: '5x7',
			shortOverlayPx: 102,
			longOverlayPx: 142,
			price: 5
		},
		{
			name: '4&#8221; x 6&#8221;',
			shortIn: 4,
			longIn: 6,
			overlayName: '4x6',
			shortOverlayPx: 82,
			longOverlayPx: 122,
			price: 3
		},
		{
			name: '8 Wallets',
			shortIn: 2.5,
			longIn: 3.5,
			overlayName: 'wallet',
			shortOverlayPx: 202,
			longOverlayPx: 142,
			price: 10
		}
		],
		photos: {

			},
		photoChooser: {
			current: {
				number: 1,
				size: 0,
				qty: 1,
				color: true,
				price: 20,
				src: 'lib/timthumb.php?src=/photos/000000/1.jpg&w=280&h=280&f=&a=',
				thumbSrc: 'lib/timthumb.php?src=/photos/000000/1.jpg&w=75&h=50&f=&a=',
				prettyPrice: '$20.<sup>00</sup>'
			},
			update: function(){
				var colorFilter = session.photoChooser.current.color ? '': '2';

				if (session.photos[session.photoChooser.current.number].horiz === false){
					session.photoChooser.current.src = 'lib/timthumb.php?src=/photos/' + session.ID + '/' + session.photoChooser.current.number + '.jpg&w=' + session.photoSizes[session.photoChooser.current.size].shortIn * 20 + '&h=' + session.photoSizes[session.photoChooser.current.size].longIn * 20 + '&f=' + colorFilter + '&a=' + session.photos[session.photoChooser.current.number].a;
					session.photoChooser.current.thumbSrc = 'lib/timthumb.php?src=/photos/' + session.ID + '/' + session.photoChooser.current.number + '.jpg&w=50&h=75&f=&a=';
					$('#photoChooser-preview').attr({
						width: session.photoSizes[session.photoChooser.current.size].shortOverlayPx,
						height: session.photoSizes[session.photoChooser.current.size].longOverlayPx,
						src: './style/overlay/' + session.photoSizes[session.photoChooser.current.size].overlayName + '_vert.png'
					})
				}
				if (session.photos[session.photoChooser.current.number].horiz === true){
					session.photoChooser.current.src = 'lib/timthumb.php?src=/photos/' + session.ID + '/' + session.photoChooser.current.number + '.jpg&w=' + session.photoSizes[session.photoChooser.current.size].longIn * 20 + '&h=' + session.photoSizes[session.photoChooser.current.size].shortIn * 20 + '&f=' + colorFilter + '&a=' + session.photos[session.photoChooser.current.number].a;
					session.photoChooser.current.thumbSrc = 'lib/timthumb.php?src=/photos/' + session.ID + '/' + session.photoChooser.current.number + '.jpg&w=75&h=50&f=&a=';
					$('#photoChooser-preview').attr({
						width: session.photoSizes[session.photoChooser.current.size].longOverlayPx,
						height: session.photoSizes[session.photoChooser.current.size].shortOverlayPx,
						src: "./style/overlay/" + session.photoSizes[session.photoChooser.current.size].overlayName + "_horiz.png",
					})
				}
				session.photoChooser.current.prettyPrice = '$' + (session.photoChooser.current.qty * session.photoSizes[session.photoChooser.current.size].price).toFixed(2).split('.')[0] + '.<sup>' + (session.photoChooser.current.qty * session.photoSizes[session.photoChooser.current.size].price).toFixed(2).split('.')[1] + '</sup>';

				//Update the display of all the info
				$('#photoChooser-price').html(session.photoChooser.current.prettyPrice);
				$('#photoChooser .photo.counter').html('Photo ' + session.photoChooser.current.number);
				$('#photoChooser .qty.counter').html('Prints: ' + session.photoChooser.current.qty);
				$('#photoChooser .size.counter').html(session.photoSizes[session.photoChooser.current.size].name);
				$('#photoChooser .color.toggle').html(session.photoChooser.current.color ? 'Color': 'Black and White');
				$('#photoChooser-preview').css('background-image', 'url(' + session.photoChooser.current.src + ')');

				if (session.editingItem === true){
					$('#photoChooser .toggle.add').html('Save Changes');
					session.cart.updateItem();
				}
				return true;
			}

		},
		editingItem: false,
		editingWhichItem: 0,
		cart: {
			items: {
				total: 0
			},
			addItem: function(){
				session.cart.items.total++;
				//There has to be a more elegant way to prevent items from updating with photoChooser.current changing...
				session.cart.items['item-' + session.cart.items.total] = JSON.parse(JSON.stringify(session.photoChooser.current));
				if ($('body').scrollTop() === 0){
					$('#cartItem-tmpl').tmpl(session).prependTo('#cart ul').hide().slideDown(500);
				} else{
					$('body').animate({
						scrollTop: 0
					},
					500,
					function(){
						$('#cartItem-tmpl').tmpl(session).prependTo('#cart ul').hide().slideDown(500);
					});
				}
				session.photoChooser.update();
			},
			removeItem: function(item){
				if (session.editingItem){
					session.cart.saveItem();
				}
				delete session.cart.items['item-' + item];
				$('#cart li.item-' + item).slideUp(500,
				function(){
					$(this).remove();
				});
				session.cart.items.total--;
			},
			editItem: function(){
				session.editingItem = true;
				$('#current-edit').attr('id', '');
				$(this).parent().parent().attr('id', 'current-edit');
				session.editingWhichItem = $('#current-edit').attr('class').split('item-')[1];
				session.photoChooser.current = JSON.parse(JSON.stringify(session.cart.items['item-' + session.editingWhichItem]));
				$('#photoChooser .add.button').removeClass('add').addClass('save').attr('title', 'Save changes to your selection and continue choosing prints').html('Save Changes');
				$('#photoChooser').css({
						position: 'absolute',
						top: (session.editingItem) ? $('#photoChooser').offset().top : $('body').scrollTop()
					}).addClass('edit').animate({
						right: -33
					},
					100,
					function(){
						$('#photoChooser').animate({
							top: $('#current-edit').offset().top - 3
						},
						500,
						function(){
							session.photoChooser.update();
						});
					});
			},
			saveItem: function(){
				session.editingItem = false;
				$('#current-edit').attr('id', '');
				$('#photoChooser').animate({
					right: -46
				},
				100,
				function(){
					$('#photoChooser').animate({
						top: $('body').scrollTop() - 4
					},
					500,
					function(){
						$('#photoChooser').removeClass('edit').removeAttr('style');
						session.photoChooser.update();
					});
				});
				$('#photoChooser .save.button').removeClass('save').addClass('add').attr('title', 'Add this selection to your cart and continue choosing prints').html('Add to Cart');
			},
			updateItem: function(){
				session.editingWhichItem = $('#current-edit').attr('class').split('item-')[1];
				session.cart.items['item-' + session.editingWhichItem] = JSON.parse(JSON.stringify(session.photoChooser.current));
				$('#current-edit').replaceWith($('#cartItem-tmpl').tmpl(session));
				$('.item-' + session.editingWhichItem).attr('id', 'current-edit');
			}
		}

	};

	$('body').delegate('.color.button', 'click',
	function(){
		session.photoChooser.current.color = (session.photoChooser.current.color === true) ? false: true;
		session.photoChooser.update();
	});
	$('body').delegate('.next.button', 'click',
	function(){
		(session.photoChooser.current.number < session.photos.total) ? session.photoChooser.current.number++:session.photoChooser.current.number = 1;
		session.photoChooser.update();
	});
	$('body').delegate('.previous.button', 'click',
	function(){
		(session.photoChooser.current.number > 1) ? session.photoChooser.current.number--:session.photoChooser.current.number = session.photos.total;
		session.photoChooser.update();
	});
	$('body').delegate('.larger.button', 'click',
	function(){
		//(photoSize > 0) ? photoSize-- : photoSize = photoSizes.length - 1;
		(session.photoChooser.current.size > 0) ? session.photoChooser.current.size--:session.photoChooser.current.size = session.photoSizes.length - 1;
		session.photoChooser.update();
	});
	$('body').delegate('.smaller.button', 'click',
	function(){
		//(photoSize < photos.length + 1) ? photoSize++ : photoSize = 0;
		(session.photoChooser.current.size < session.photoSizes.length - 1) ? session.photoChooser.current.size++:session.photoChooser.current.size = 0;
		session.photoChooser.update();
	});
	$('body').delegate('.more.button', 'click',
	function(){
		session.photoChooser.current.qty++;
		session.photoChooser.update();
	});
	$('body').delegate('.fewer.button', 'click',
	function(){
		(session.photoChooser.current.qty > 1) ? session.photoChooser.current.qty--:session.photoChooser.current.qty;
		session.photoChooser.update();
	});
	$('body').delegate('.add.button', 'click', session.cart.addItem);
	$('body').delegate('.save.button', 'click', session.cart.saveItem);
	$('body').delegate('.remove.button', 'click',
	function(){
		session.cart.removeItem($(this).attr('class').split('remove-')[1]);
	});
	$('body').delegate('.edit.button', 'click', session.cart.editItem);
	$('#login input[type=text], #login input[type=password]').val('');
	$('#login').submit(function(){
		var sessionID = $('#sessionID').val();
		var sessionPassword = $('#sessionPassword').val();
		$.ajax({
			cache: false,
			dataType: 'xml',
			type: 'GET',
			url: 'photos/' + sessionID + '/client.xml',
			username: sessionID,
			password: sessionPassword,
			error: function(){
				alert('Session ID or Password is Incorrect, please try again.');
			},
			success: session.init
		});
		return false;
	});
});

//Disable text selection from code.jdempster.com/jQuery.DisableTextSelect
(function($){if($.browser.mozilla){$.fn.disableTextSelect=function(){return this.each(function(){$(this).css({"MozUserSelect":"none"})})};$.fn.enableTextSelect=function(){return this.each(function(){$(this).css({"MozUserSelect":""})})}}else{if($.browser.msie){$.fn.disableTextSelect=function(){return this.each(function(){$(this).bind("selectstart.disableTextSelect",function(){return false})})};$.fn.enableTextSelect=function(){return this.each(function(){$(this).unbind("selectstart.disableTextSelect")})}}else{$.fn.disableTextSelect=function(){return this.each(function(){$(this).bind("mousedown.disableTextSelect",function(){return false})})};$.fn.enableTextSelect=function(){return this.each(function(){$(this).unbind("mousedown.disableTextSelect")})}}}})(jQuery)